package com.gzyj.server.factory.jtt808.up;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.v2011.protocol.terminalparam.*;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import com.gzyj.jtt808.v2013.enums.JT808TerminalResult;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x0104;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x8001;
import com.gzyj.pojo.anbiao.AnbiaoTerminalParamTx;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.mapper.AnbiaoTerminalParamTxMapper;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class JTT808_0x0104_Handler implements CommonHandler {

    @Resource(name = "clusterRedisTemplate")
    private RedisTemplate<String, Object> clusterRedisTemplate;
    @Value("${redis.key.vehicle.cmdrs}")
    private String VehicleZdCMDRS;

    @Autowired
    private AnbiaoTerminalParamTxMapper anbiaoTerminalParamTxMapper;

    private AnbiaoTerminalParamTx setTxParam(AnbiaoTerminalParamTx anbiaoTerminalParamTx, List<JTT808_0x8103_TerminalParam> params) {
        for (JTT808_0x8103_TerminalParam param : params) {
            if (param.getParamId() == 0x0002) {
                JTT808_0x8103_0x0002 p = (JTT808_0x8103_0x0002) param;
                anbiaoTerminalParamTx.setTcpTimeout(p.getParamValue());
            }
            if (param.getParamId() == 0x0003) {
                JTT808_0x8103_0x0003 p = (JTT808_0x8103_0x0003) param;
                anbiaoTerminalParamTx.setTcpR(p.getParamValue());
            }
            if (param.getParamId() == 0x0004) {
                JTT808_0x8103_0x0004 p = (JTT808_0x8103_0x0004) param;
                anbiaoTerminalParamTx.setUdpTimeout(p.getParamValue());
            }
            if (param.getParamId() == 0x0005) {
                JTT808_0x8103_0x0005 p = (JTT808_0x8103_0x0005) param;
                anbiaoTerminalParamTx.setUdpR(p.getParamValue());
            }
            if (param.getParamId() == 0x0006) {
                JTT808_0x8103_0x0006 p = (JTT808_0x8103_0x0006) param;
                anbiaoTerminalParamTx.setSmsTimeout(p.getParamValue());
            }
            if (param.getParamId() == 0x0007) {
                JTT808_0x8103_0x0007 p = (JTT808_0x8103_0x0007) param;
                anbiaoTerminalParamTx.setSmsR(p.getParamValue());
            }
            if (param.getParamId() == 0x0010) {
                JTT808_0x8103_0x0010 p = (JTT808_0x8103_0x0010) param;
                anbiaoTerminalParamTx.setMasterApn(p.getParamValue());
            }
            if (param.getParamId() == 0x0011) {
                JTT808_0x8103_0x0011 p = (JTT808_0x8103_0x0011) param;
                anbiaoTerminalParamTx.setMasterUserName(p.getParamValue());
            }
            if (param.getParamId() == 0x0012) {
                JTT808_0x8103_0x0012 p = (JTT808_0x8103_0x0012) param;
                anbiaoTerminalParamTx.setMasterPassword(p.getParamValue());
            }
            if (param.getParamId() == 0x0013) {
                JTT808_0x8103_0x0013 p = (JTT808_0x8103_0x0013) param;
                String[] ips = p.getParamValue().split(";");
                if (ips.length > 0) {
                    String ipp = ips[0];
                    String[] ippp = ipp.split(":");
                    if (ippp.length > 1) {
                        anbiaoTerminalParamTx.setMasterIp(ippp[0]);
                        anbiaoTerminalParamTx.setMasterPort(Integer.valueOf(ippp[1]));
                    } else {
                        anbiaoTerminalParamTx.setMasterIp(ipp);
                    }

                }

            }
            if (param.getParamId() == 0x0014) {
                JTT808_0x8103_0x0014 p = (JTT808_0x8103_0x0014) param;
                anbiaoTerminalParamTx.setSlaveApn(p.getParamValue());
            }
            if (param.getParamId() == 0x0015) {
                JTT808_0x8103_0x0015 p = (JTT808_0x8103_0x0015) param;
                anbiaoTerminalParamTx.setSlaveUserName(p.getParamValue());
            }
            if (param.getParamId() == 0x0016) {
                JTT808_0x8103_0x0016 p = (JTT808_0x8103_0x0016) param;
                anbiaoTerminalParamTx.setSlavePassword(p.getParamValue());
            }
            if (param.getParamId() == 0x0017) {
                JTT808_0x8103_0x0017 p = (JTT808_0x8103_0x0017) param;
                String[] ips = p.getParamValue().split(";");
                if (ips.length > 0) {
                    String ipp = ips[0];
                    String[] ippp = ipp.split(":");
                    if (ippp.length > 1) {
                        anbiaoTerminalParamTx.setSlaveIp(ippp[0]);
                        anbiaoTerminalParamTx.setSlavePort(Integer.valueOf(ippp[1]));
                    } else {
                        anbiaoTerminalParamTx.setSlaveIp(ipp);
                    }

                }
            }
        }
        return anbiaoTerminalParamTx;
    }

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {
        JTT808_0x0104 jtt808_0x0104 = (JTT808_0x0104) msg;

        long c = jtt808_0x0104.getTerminalParams().stream().filter(e -> e.getParamId() == 0x0002).count();
        if (c > 0) {


            AnbiaoTerminalParamTx anbiaoTerminalParamTx = anbiaoTerminalParamTxMapper.selectOne(new QueryWrapper<AnbiaoTerminalParamTx>().eq("simNo", jtt808_0x0104.getPackageHeader().getTerminalPhoneNo()));
            if (anbiaoTerminalParamTx != null) {
                setTxParam(anbiaoTerminalParamTx, jtt808_0x0104.getTerminalParams());
                anbiaoTerminalParamTxMapper.updateById(anbiaoTerminalParamTx);
            } else {
                AnbiaoTerminalParamTx terminalParamTx = new AnbiaoTerminalParamTx();
                setTxParam(terminalParamTx, jtt808_0x0104.getTerminalParams());
                terminalParamTx.setSimNo(jtt808_0x0104.getPackageHeader().getTerminalPhoneNo());
                anbiaoTerminalParamTxMapper.insert(terminalParamTx);
            }


        }


        JTT808_0x8001 jtt808_0x8001 = new JTT808_0x8001();
        HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
        headerMsgBodyProperty.setSubcontract(jtt808_0x0104.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(jtt808_0x0104.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(jtt808_0x0104.getPackageHeader().getTerminalPhoneNo());

        jtt808_0x8001.setMsgNum(jtt808_0x0104.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(jtt808_0x0104.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);

        ctx.writeAndFlush(jtt808_0x8001.encode());
        ctx.flush();

        clusterRedisTemplate.opsForValue().set(VehicleZdCMDRS + jtt808_0x0104.getPackageHeader().getTerminalPhoneNo() + ":" + 0x0104 + ":" + jtt808_0x0104.getMsgNum(), jtt808_0x0104, 10, TimeUnit.MINUTES);

        LogConfig.commondLog.info("808 2013版 {} 指令 解析完成", BusinessDataType.find(jtt808_0x0104.getPackageHeader().getMsgId()));
    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {

    }
}
